package com.zd.wechatPlatform.common.utils;

import java.net.URLEncoder;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 微信公众号生成菜单
 * @author wangliming
 *
 * @date 2018年11月1日下午3:39:07
 */
public class GenMenuUtil {
	private static final String CREATE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=";
	
	public static void main(String[] args) throws Exception{
		String appId = "wxd185421b20501a5d";
		String accessToken = "15_aQ6NJOf9ZYDQwCNa9KJwUdmU32t-5VkzMsNRLB0B5psG0Ykp2qu9dP-o1HwjR9N1bOQEqbwQvxNIwC9CptIyHnMsOXzG0WvMivQTlVbMBCZfemfiELez9FHcM4HOWS5FgF0S6Wk-7UC86MuKEFBaAJAOPL";
		
		JSONObject menu = new JSONObject();
		JSONArray menuList = new JSONArray();

		//=====================================第一列菜单
		JSONObject firstMenu = new JSONObject();
		firstMenu.put("type", "view");
		firstMenu.put("name", "百度");
		firstMenu.put("url", genUrl("http://www.baidu.com",appId));
		firstMenu.put("sub_button", new JSONArray());
		menuList.add(firstMenu);

		//=====================================第二列菜单
		JSONObject secMenu = new JSONObject();
		secMenu.put("name", "绑定车牌");
		secMenu.put("type", "view");
		secMenu.put("url", genUrl("http://www.baidu.com",appId));
		secMenu.put("sub_button", new JSONArray());
		menuList.add(secMenu);
		
		//=====================================第三列菜单
		JSONObject threeMenu = new JSONObject();
		threeMenu.put("name", "更多");
		
		JSONArray threeMenuList = new JSONArray();
		JSONObject threeSecMenu = new JSONObject();
		threeSecMenu.put("type", "view");
		threeSecMenu.put("name", "我的停车劵");
		threeSecMenu.put("url", genUrl("http://www.baidu.com",appId));
		threeSecMenu.put("sub_button", new JSONArray());
		threeMenuList.add(threeSecMenu);
		
		JSONObject threeOneMenu = new JSONObject();
		threeOneMenu.put("name", "发票申领");
		threeOneMenu.put("type", "view");
		threeOneMenu.put("url", genUrl("http://www.baidu.com",appId));
		threeOneMenu.put("sub_button", new JSONArray());
		threeMenuList.add(threeOneMenu);
		
		JSONObject threeThreeMenu = new JSONObject();
		threeThreeMenu.put("name", "关于慧泊客");
		threeThreeMenu.put("type", "click");
		threeThreeMenu.put("key", "guanyuhuiboke");
		threeThreeMenu.put("sub_button", new JSONArray());
		threeMenuList.add(threeThreeMenu);
		
		JSONObject threeFourMenu = new JSONObject();
		threeFourMenu.put("name", "联系我们");
		threeFourMenu.put("type", "click");
		threeFourMenu.put("key", "lianxiwomen");
		threeFourMenu.put("sub_button", new JSONArray());
		threeMenuList.add(threeFourMenu);
		threeMenu.put("sub_button", threeMenuList);
		menuList.add(threeMenu);
		
		menu.put("button", menuList);
		
		String genUrl = CREATE_MENU_URL + accessToken;
        System.out.println( OkHttpUtil.postSync(genUrl, menu.toJSONString()));
	}

	private static String genUrl(String url, String appId) throws Exception {
		String redirectUri = "http://test.wx.boshukeji.com/redirectManager?url=" + url;
		return "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appId + "&redirect_uri="
				+ URLEncoder.encode(redirectUri, "UTF-8") + "&response_type=code&scope=snsapi_base&state=" + appId
				+ "#wechat_redirect";
	}
}
